package com.ktjiaoyu.crm;

import com.ktjiaoyu.crm.pojo.Role;
import com.ktjiaoyu.crm.pojo.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTemplateTester {
    @Resource
    private StringRedisTemplate stringRedisTemplate;//操作字符串数据
    @Resource
    private RedisTemplate redisTemplate;//操作其他数据类型

    @Test
    public void testStringRedisTemplate(){
        stringRedisTemplate.opsForValue().set("test", "100",60*10, TimeUnit.SECONDS);//向 redis 里存入数据和设置缓存时间
        stringRedisTemplate.boundValueOps("test").increment(-1);//val 做-1 操作
        stringRedisTemplate.opsForValue().get("test");//根据 key 获取缓存中的 val
        stringRedisTemplate.boundValueOps("test").increment(1);//val +1
        stringRedisTemplate.getExpire("test");//根据 key 获取过期时间
        stringRedisTemplate.getExpire("test",TimeUnit.SECONDS);//根据 key 获取过期时间并换算 成指定单位
         stringRedisTemplate.delete("test");//根据 key 删除缓存
         stringRedisTemplate.hasKey("546545");//检查 key 是否存在，返回 boolean 值
         stringRedisTemplate.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合
         stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//设置过期时间
         stringRedisTemplate.opsForSet().isMember("red_123", "1");//根据key查看集合中是否存 在指定数据
         stringRedisTemplate.opsForSet().members("red_123");//根据 key 获取 set 集合
    }


    @Test
    public void testString() {
        stringRedisTemplate.opsForValue().set("name", "czkt");
        Assert.assertEquals("czkt", stringRedisTemplate.opsForValue().get("name"));
    }

    //操作实体：
    @Test
     public void testObj(){
         User user=new User();
         Role role=new Role();
         role.setRoleId(1L);
         user.setUsrName("czkt");
         user.setUsrPassword("123456");
//         user.getRole().setRoleId(1L);
         user.setUsrFlag(null);
         ValueOperations<String, User> operations=redisTemplate.opsForValue();
         operations.set("com.ktjiaoyu.crm.pojo", user);
         User u=operations.get("com.ktjiaoyu.crm.pojo");
         System.out.println("user.usrName: "+u.getUsrName());
    }

    //超时失效：
    @Test
     public void testExpire() throws InterruptedException {
        User user=new User();
        Role role=new Role();
        role.setRoleId(1L);
        user.setUsrName("czkt");
        user.setUsrPassword("123456");
//        user.getRole().setRoleId(1L);
        user.setUsrFlag(null);
        ValueOperations<String, User> operations = redisTemplate.opsForValue();
        operations.set("expire", user, 100, TimeUnit.MILLISECONDS);
        Thread.sleep(1000);
        boolean exists = redisTemplate.hasKey("expire");
        if (exists) {
            System.out.println("exists is true");
        } else {
            System.out.println("exists is false");
        }
    }
//    操作哈希 (Hash)
     @Test
     public void testHash() {
         HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
         hash.put("hash","name","ktjiaoyu");
         String value=(String) hash.get("hash","name");
         System.out.println("hash name'value :"+value);
    }
    //操作列表
    @Test
    public void testList(){
        ListOperations<String,String> list=redisTemplate.opsForList();
        list.leftPush("list","accp");
        list.leftPush("list","bdqn");
        list.leftPush("list","czkt");
        String value=(String)list.leftPop("list");
        System.out.println("list value:"+value.toString());
    }
}
